#coding:utf-8
import os
import time
from common.koufuApi import KoufuApi

__author__ = 'yangxianming'

from common.KoufuConf import KoufuConf
import json
import requests


def case():
    #初始化必要的参数
    kou_fu_conf = KoufuConf()
    test_url = kou_fu_conf.koufu_prod_domain + "/api/koufu"
    kou_fu_api = KoufuApi()
    script_name = os.path.basename(__file__)
    tf = True
    test_result = ""
    mealRuleId = ""

    try:
        #查询用户余额
        p = kou_fu_api.get_efuli_koufu_userMeal_enable_voucherQuery_data()
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_userMeal_enable_voucherQuery返回的全部参数\n", r.content.decode("utf-8"))
        before_balance = r.json()["return_data"]["totalMoney"]

        #创建餐规
        mark = "节日祝福" + str(int(time.time() * 1000))
        delete_keys = "autoSend,sendOutPunchCard,overtimePunchTime,sendDaysDetail,ruleAddressTag,ruleAddress,latitude,longitude,dingtalkUsercount,cityId,cityName,mealRuleId,shopArray,ruleAddressLabel,addressKeyword,rangeMeters,quantity"
        p = kou_fu_api.get_efuli_koufu_mealrule_merge_data(update_body={"ruleType":"03","ruleName":mark,"voucherAmount":400,"sendDays":"ON"}, del_body_key=delete_keys)
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_mealrule_merge返回的全部参数\n", r.content.decode("utf-8"))
        mealRuleId = r.json()["mealRuleId"]

        #调用发放接口
        p = kou_fu_api.get_efuli_koufu_mealrule_send_data(update_body={"mealRuleId":mealRuleId})
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_mealrule_send返回的全部参数\n", r.content.decode("utf-8"))

        #查询用户余额
        p = kou_fu_api.get_efuli_koufu_userMeal_enable_voucherQuery_data()
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_userMeal_enable_voucherQuery返回的全部参数\n", r.content.decode("utf-8"))
        after_balance = r.json()["return_data"]["totalMoney"]

        test_result = after_balance - before_balance
        if test_result == 400:
            tf = True
        print("验证余额是否增加400")
    except Exception as e:
        err_info = str(e).replace("'", "").replace("\"", "")
        test_result = "非预期的异常,脚本文件:" + script_name + "; 错误信息：" + err_info
        tf = -2

    #定义预期描述
    print(str(tf))
    print(test_result)
    expect_result = "验证余额是否增加400"

    #返回测试结果 第一个参数:True or False    第二个参数测试结果描述
    return tf, [test_result, expect_result]

# case()